package com.cskaoyan.service;

import cn.hutool.core.date.format.DateBasic;
import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.goods.MarketGoods;
import com.cskaoyan.bean.goods.MarketGoodsExample;
import com.cskaoyan.bean.market.*;
import com.cskaoyan.bean.order.*;
import com.cskaoyan.bean.user.User;
import com.cskaoyan.mapper.AftersaleMapper;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.OrdergoodsMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.mapper.orderMapper.MarkeChannelMapper;
import com.cskaoyan.mapper.orderMapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.orderMapper.MarketOrderMapper;
import com.cskaoyan.util.DateUtils;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;

    @Autowired
    MarkeChannelMapper markeChannelMapper;

    @Autowired
    MarketCommentMapper commentMapper;


    @Override
    public OrderDetail detail(Integer orderId) {
//        private MarketOrder order;
        MarketOrder marketOrder = orderMapper.selectByPrimaryKey(orderId);
        Integer userId = marketOrder.getUserId();

//        private User user;
        User user = userMapper.selectByPrimaryKey(userId);

//        private List<MarketOrderGoods> orderGoods;
        List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByOrderId(orderId);

        OrderDetail orderDetail = new OrderDetail(user, marketOrder, marketOrderGoods);
        return orderDetail;

    }

    @Override
    public BaseData<MarketOrder> list(BaseParam baseParam, String start, String end, Short[] orderStatusArray, Integer userId, Integer orderId, String orderSn) {
        PageUtil.start(baseParam);


        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (start != null && !"".equals(start)) {
            Date dateStart = DateUtils.parseDate(start);
            Date dateEnd = DateUtils.parseDate(end);
            criteria.andAddTimeBetween(dateStart, dateEnd);
        }

        if (orderStatusArray != null) {
            criteria.andOrderStatusIn(Arrays.asList(orderStatusArray));
        }

        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (orderId != null && !"".equals(orderId)) {
            criteria.andIdEqualTo(orderId);
        }
        if (orderSn != null && !"".equals(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn);
        }
        List<MarketOrder> marketOrders = orderMapper.selectByExample(marketOrderExample);
        PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);
        BaseData<MarketOrder> data = new BaseData<>(pageInfo, marketOrders);
        return data;
    }

    @Override
    public void ship(Integer orderId, String shipChannel, String shipSn) {
        Date date = new Date(System.currentTimeMillis());
        orderMapper.updateCauseByShip(orderId, shipChannel, shipSn, date);
    }

    @Override

    public void refund(Integer orderId, BigDecimal refundMoney) {
        Date date = new Date(System.currentTimeMillis());
        orderMapper.updateCauseByRefund(orderId, refundMoney, date);
    }

    @Override
    public List<Channel> channel() {
        List<Channel> channels = markeChannelMapper.selectALL();
        return channels;
    }

    @Override
    public int reply(Integer commentId, String content) {
        //更新时间
        Date date = new Date(System.currentTimeMillis());
        int reply = commentMapper.reply(commentId, content, date);
        return reply;
    }

}
